package com.live.dengdengtv.activity.live;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.live.dengdengtv.BaseActivity;
import com.live.dengdengtv.R;
import com.live.dengdengtv.activity.AddTopicActivity;
import com.live.dengdengtv.activity.support.CurLiveInfo;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.LoginInfo;
import com.live.dengdengtv.bean.Constants;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wyf on 2016/8/14.
 */
public class ActivityLiveTitle extends BaseActivity implements View.OnClickListener{

    private static final String TAG = "ActivityLiveTitle";
    private final int REQUEST_PHONE_PERMISSIONS = 0;
    private EditText title_et;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_input_livetitle);

        title_et = (EditText) findViewById(R.id.title_et);
        checkPermission();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_back: finish();break;
            case R.id.addTopic_tv: goAddTopic();break;
//            case R.id.imageView_back: finish();break;
//            case R.id.imageView_back: finish();break;
//            case R.id.imageView_back: finish();break;
            case R.id.start_singlelive_bt: goRoom();break;
        }
    }

    private void goAddTopic() {
        Intent intent = new Intent(context, AddTopicActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String topic = data.getStringExtra("topic");
            title_et.setText(topic);
        }
    }

    private void goRoom(){

//        Intent intent = new Intent(this, LiveActivity.class);
        Intent intent = new Intent(this, LiveingActivity.class);
        intent.putExtra(Constants.ID_STATUS, Constants.HOST);
        CurLiveInfo.setIdStatus(Constants.HOST);
        CurLiveInfo.setJoinRoomWay(true);
        String title = title_et.getText().toString();
        if(TextUtils.isEmpty(title)){
            CurLiveInfo.setTitle("快来看直播啦!");
        }else {
            CurLiveInfo.setTitle(title);
        }

        CurLiveInfo.setHostID(LoginInfo.getInstance().getUserId());
        CurLiveInfo.setHostName(LoginInfo.getInstance().getUserNickname());
        CurLiveInfo.setRoomNum(LoginInfo.getInstance().getRoomId());
        CurLiveInfo.setAvatorurl(AppInfo.loginInfo.getAvatarAddress());

//        AppInfo.setLiveTitle(title);

        startActivity(intent);


        finish();
    }

    private void checkPermission() {
        final List<String> permissionsList = new ArrayList<String>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if ((checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.CAMERA);
            if ((checkSelfPermission(Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.RECORD_AUDIO);
            if ((checkSelfPermission(Manifest.permission.WAKE_LOCK) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.WAKE_LOCK);
            if ((checkSelfPermission(Manifest.permission.MODIFY_AUDIO_SETTINGS) != PackageManager.PERMISSION_GRANTED))
                permissionsList.add(Manifest.permission.MODIFY_AUDIO_SETTINGS);
            if (permissionsList.size() != 0) {
                requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                        REQUEST_PHONE_PERMISSIONS);
            }
        }
    }
}
